﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Collections.Specialized;

namespace RAP.Framework.Libary.Utils
{
  /// <summary>
  /// 数组对象扩展
  /// </summary>
  public static partial class Extensions
  {
    #region 数组对象扩展

    /// <summary>
    /// 将指定的字节数组转换成字符串，并且指定是否使用GZIP解压，默认不解压
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="useGZIP"></param>
    /// <returns></returns>
    public static string ToText(this byte[] arr, bool useGZIP = false, Encoding enc = null)
    {
      if (arr.IsNullOrEmpty())
      {
        return null;
      }
      enc = enc != null ? enc : Encoding.UTF8;
      if (!useGZIP)
      {
        return enc.GetString(arr).TrimStart((char)65279);
      }
      using (GZipStream zip = new GZipStream(new MemoryStream(arr), CompressionMode.Decompress, true))
      {
        return zip.ReadAllText();
      }
    }

    /// <summary>
    /// 循环集合的所有元素，并对每一个元素执行指定的Action
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="fun"></param>
    public static void Map<T>(this IEnumerable arr, Action<T> fun)
    {
      if (arr != null)
      {
        foreach (T item in arr)
        {
          fun(item);
        }
      }
    }

    /// <summary>
    /// 循环集合的所有元素，并对每一个元素执行指定的Action
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="fun"></param>
    public static void Map(this IEnumerable arr, Func<object, bool> fun)
    {
      if (arr != null)
      {
        foreach (object item in arr)
        {
          if (!fun(item))
          {
            return;
          }
        }
      }
    }

    /// <summary>
    /// 循环集合的所有元素，并对每一个元素执行指定的Action
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="arr"></param>
    /// <param name="fun"></param>
    public static void Map<T>(this IEnumerable arr, Func<T, bool> fun)
    {
      if (arr != null)
      {
        foreach (T item in arr)
        {
          if (!fun(item))
          {
            return;
          }
        }
      }
    }

    /// <summary>
    /// 循环集合的所有元素，并对每一个元素执行指定的Func，并返回一个新的结果集
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="fun"></param>
    /// <param name="ignoreEmpty">是否忽略空值</param>
    /// <returns></returns>
    public static IList Map(this IEnumerable arr, Func<object, object> fun, bool ignoreEmpty = false)
    {
      if (arr == null)
      {
        return null;
      }
      IList result = new ArrayList();

      foreach (object item in arr)
      {
        var val = fun(item);
        if (!ignoreEmpty || (ignoreEmpty && val.IsNotNullOrEmpty()))
        {
          result.Add(val);
        }
      }
      return result;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TIn"></typeparam>
    /// <typeparam name="TOut"></typeparam>
    /// <param name="arr"></param>
    /// <param name="fun"></param>
    /// <param name="ignoreEmpty"></param>
    /// <returns></returns>
    public static IList<TOut> Map<TIn, TOut>(this IEnumerable arr, Func<TIn, TOut> fun, bool ignoreEmpty = false)
    {
      if (arr == null)
      {
        return null;
      }
      IList<TOut> result = new List<TOut>();
      Type typeIn = typeof(TIn);
      foreach (var item in arr)
      {
        TIn itemValue = (item is IConvertible) ? (TIn)(Convert.ChangeType(item, typeIn)) : (TIn)item;
        var val = fun(itemValue);
        if (!ignoreEmpty || (ignoreEmpty && val.IsNotNullOrEmpty()))
        {
          result.Add(val);
        }
      }
      return result;
    }

    /// <summary>
    /// 返回枚举、集合的第一个元素
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="items"></param>
    /// <returns></returns>
    public static T FirstItem<T>(this IEnumerable<T> items)
    {
      if (items.IsNullOrEmpty())
      {
        return default(T);
      }
      return items.First();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="items"></param>
    /// <returns></returns>
    public static object FirstItem(this IEnumerable items)
    {
      if (items == null)
      {
        return null;
      }
      return items.Cast<object>().First();
    }

    #region 数据分组、合计函数

    #region 常量定义
    /// <summary>
    /// 字段名称：FieldName
    /// </summary>
    public const string GROUP_FIELD_NAME = "FieldName";

    /// <summary>
    /// 字段别名：FieldAlias
    /// </summary>
    public const string GROUP_FIELD_ALIAS = "FieldAlias";

    /// <summary>
    /// 排序：Sort
    /// </summary>
    public const string GROUP_SORT = "Sort";

    /// <summary>
    /// 合计表达式：Aggregate
    /// </summary>
    public const string GROUP_AGGREGATE = "Aggregate";

    /// <summary>
    /// 分组数据行：IsGroupRow
    /// </summary>
    public const string GROUP_IS_GROUP_ROW = "IsGroupRow";

    /// <summary>
    /// 分组数据明细：GroupData
    /// </summary>
    public const string GROUP_DATA = "GroupedData";

    /// <summary>
    /// 分组数据Key：GroupedKey
    /// </summary>
    public const string GROUP_KEY = "GroupedKey";

    /// <summary>
    /// 合计函数名称：Function
    /// </summary>
    public const string GROUP_FUNCTION = "Function";

    /// <summary>
    /// 分组表达式：GroupExpression
    /// </summary>
    public const string GROUP_EXPRESSION = "GroupExpression";

    /// <summary>
    /// 求和：SUM
    /// </summary>
    public const string GROUP_AGGREGATE_FUNCTION_SUM = "Sum";

    /// <summary>
    /// 求最大值：MAX
    /// </summary>
    public const string GROUP_AGGREGATE_FUNCTION_MAX = "Max";

    /// <summary>
    /// 计算个数：Count
    /// </summary>
    public const string GROUP_AGGREGATE_FUNCTION_COUNT = "Count";

    /// <summary>
    /// 求最小值：Min
    /// </summary>
    public const string GROUP_AGGREGATE_FUNCTION_MIN = "Min";

    /// <summary>
    /// 求平均：Average
    /// </summary>
    public const string GROUP_AGGREGATE_FUNCTION_AVG = "Average";

    /// <summary>
    /// 获取第一个：First
    /// </summary>
    public const string GROUP_AGGREGATE_FUNCTION_FIRST = "First";

    /// <summary>
    /// 获取最后一个：Last
    /// </summary>
    public const string GROUP_AGGREGATE_FUNCTION_LAST = "Last";

    /// <summary>
    /// 顺序排序：ASC
    /// </summary>
    public const string GROUP_SORT_ASC = "ASC";

    /// <summary>
    /// 倒序排序：DESC
    /// </summary>
    public const string GROUP_SORT_DESC = "DESC";
    #endregion

    /// <summary>
    /// 对指定的IDictionary数据集合进行分组、合计操作
    /// </summary>
    /// <param name="source">数据源</param>
    /// <param name="groupSettings">分组、合计表达式</param>
    /// <returns></returns>
    public static IEnumerable<IDictionary> GroupByExpressions(this IEnumerable<IDictionary> source, ICollection groupSettings)
    {
      if (groupSettings == null || groupSettings.Count == 0)
      {
        return source;
      }
      IDictionary groupSetting = null;
      IList groupSettingsClone = null;
      if (groupSettings is IList)
      {
        groupSettingsClone = new ArrayList(groupSettings as ICollection);
        groupSetting = groupSettingsClone[0] as IDictionary;
        groupSettingsClone.RemoveAt(0);
      }
      else
      {
        groupSetting = groupSettings as IDictionary;
      }

      string groupField = groupSetting.ToString(GROUP_FIELD_NAME);
      string fieldAlias = null;
      string sort = groupSetting.ToString(GROUP_SORT);
      if (string.IsNullOrEmpty(groupField))
      {
        return source.ToList();
      }

      #region 定义分组关键字选择函数
      Func<IDictionary, object> keySelector = null;
      if (groupField.IndexOf(',') < 0)
      {
        fieldAlias = groupSetting.ToString(GROUP_FIELD_ALIAS, groupField);
        keySelector = (IDictionary data) =>
        {
          return data[groupField];
        };
      }
      else
      {
        fieldAlias = groupSetting.ToString(GROUP_FIELD_ALIAS, GROUP_KEY);
        var groupKeyValueCaches = new Hashtable();
        var groupFields = groupField.Split(',');
        keySelector = (IDictionary data) =>
        {
          List<string> groupKeys = new List<string>();
          foreach (string key in groupFields)
          {
            groupKeys.Add(data.ToString(key));
          }
          string groupKey = groupKeys.Join(",");
          var groupKeyValue = groupKeyValueCaches.ToDictionary(groupKey);
          if (groupKeyValue == null)
          {
            groupKeyValue = new Hashtable();
            foreach (string key in groupFields)
            {
              groupKeyValue.Add(key, data[key]);
            }
            groupKeyValueCaches[groupKey] = groupKeyValue;
          }
          return groupKeyValue;
        };
      }
      #endregion 定义分组关键字选择函数

      IList aggregates = groupSetting.ParseToList(GROUP_AGGREGATE);

      //定义明细数据选择函数
      #region 定义明细数据选择函数
      Func<IGrouping<object, IDictionary>, IDictionary> dataSelector
          = (IGrouping<object, IDictionary> groupData) =>
          {
            IDictionary resultData = new Dictionary<string, object>();
            resultData[fieldAlias] = groupData.Key;
            resultData[GROUP_IS_GROUP_ROW] = true;//标识该对象是分组数据对象

            //递归分组、合计数据
            if (groupSettings is IList)
            {
              resultData[GROUP_DATA] = GroupByExpressions(groupData, groupSettingsClone).ToList();
            }
            else if (groupSettings is IDictionary)
            {
              resultData[GROUP_DATA] = GroupByExpressions(groupData, (groupSettings as IDictionary).ToDictionary(GROUP_EXPRESSION)).ToList();
            }
            #region 进行合计函数处理
            if (aggregates != null)
            {
              //进行合计函数处理
              foreach (IDictionary aggregate in aggregates)
              {
                string aggFun = aggregate.ToString(GROUP_FUNCTION);
                if (string.IsNullOrEmpty(aggFun))
                {
                  continue;
                }
                string aggFieldName = aggregate.ToString(GROUP_FIELD_NAME);
                string aggFieldAlias = aggregate.ToString(GROUP_FIELD_ALIAS, aggFieldName);
                switch (aggFun)
                {
                  case GROUP_AGGREGATE_FUNCTION_SUM:
                    resultData[aggFieldAlias] = groupData.Sum(data => data.ToDecimal(aggFieldName, 0));
                    break;

                  case GROUP_AGGREGATE_FUNCTION_COUNT:
                    resultData[aggFieldAlias] = groupData.Count();
                    break;

                  case GROUP_AGGREGATE_FUNCTION_AVG:
                    resultData[aggFieldAlias] = groupData.Average(data => data.ToDecimal(aggFieldName, 0));
                    break;

                  case GROUP_AGGREGATE_FUNCTION_MAX:
                    resultData[aggFieldAlias] = groupData.Max(data => data.ToDecimal(aggFieldName, 0));
                    break;

                  case GROUP_AGGREGATE_FUNCTION_MIN:
                    resultData[aggFieldAlias] = groupData.Min(data => data.ToDecimal(aggFieldName, 0));
                    break;

                  case GROUP_AGGREGATE_FUNCTION_FIRST:
                    resultData[aggFieldAlias] = groupData.First()[aggFieldName];
                    break;

                  case GROUP_AGGREGATE_FUNCTION_LAST:
                    resultData[aggFieldAlias] = groupData.Last()[aggFieldName];
                    break;
                }
              }
            }
            #endregion 进行合计函数处理
            return resultData;
          };
      #endregion 定义明细数据选择函数

      //对数据源进行分组
      var groupResult = source.GroupBy(keySelector);

      #region 对已分组数据按分组字段排序
      if (!string.IsNullOrEmpty(sort))
      {
        switch (sort.ToUpper())
        {
          case GROUP_SORT_DESC:
            //倒序排序
            groupResult = groupResult.OrderByDescending(data => data.Key);
            break;

          case GROUP_SORT_ASC:
          default:
            //顺序排序
            groupResult = groupResult.OrderBy(data => data.Key);
            break;
        }
      }
      #endregion 对已分组数据按分组字段排序

      //对已分组数据进行数据选择和合计操作
      return groupResult.Select(dataSelector);
    }

    #endregion

    /// <summary>
    /// 将集合拼接为字符串
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="separator"></param>
    /// <param name="propertyName"></param>
    /// <param name="fun"></param>
    /// <param name="startIndex"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    public static string Join(this ICollection arr, string separator = "", string propertyName = null, Func<Object, int, string> fun = null, int startIndex = 0, int count = 0)
    {
      List<string> list = new List<string>();
      bool hasProperty = !string.IsNullOrEmpty(propertyName);
      int counter = 0;
      int joinedCount = 0;
      if (count == 0)
      {
        count = arr.Count;
      }
      foreach (object item in arr)
      {
        if (counter < startIndex)
        {
          counter++;
        }
        object value;
        if (hasProperty)
        {
          value = item.GetValue(propertyName);
        }
        else
        {
          value = item;
        }
        if (fun != null)
        {
          value = fun(item, counter);
        }
        if (value.IsNotNullOrEmpty())
        {
          list.Add(value.ToString());
        }
        counter++;
        joinedCount++;
        if (joinedCount >= count)
        {
          break;
        }
      }
      return string.Join(separator, list);
    }

    /// <summary>
    /// 链接枚举成员为字符串
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="separator"></param>
    /// <param name="func"></param>
    /// <returns></returns>
    public static string Join(this IEnumerable arr, string separator = "", Func<object, int, string> func = null)
    {
      List<string> result = new List<string>();
      IEnumerator e = arr.GetEnumerator();
      if (func == null)
      {
        func = (object value, int idx) =>
        {
          if (value == null || value.ToString() == string.Empty)
          {
            return null;
          }
          return value.ToString();
        };
      }
      int index = 0;
      while (e.MoveNext())
      {
        string val = func(e.Current, index);
        if (!string.IsNullOrEmpty(val))
        {
          result.Add(val);
        }
        index++;
      }
      return string.Join(separator, result.ToArray());
    }

    /// <summary>
    /// 链接枚举成员为字符串
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <param name="separator"></param>
    /// <param name="func"></param>
    /// <returns></returns>
    public static string Join<T>(this IEnumerable list, string separator = "", Func<T, int, string> func = null)
    {
      List<string> result = new List<string>();
      IEnumerator e = list.GetEnumerator();
      if (func == null)
      {
        func = (T value, int idx) =>
        {
          if (value == null || value.ToString() == string.Empty)
          {
            return null;
          }
          return value.ToString();
        };
      }
      int index = 0;
      while (e.MoveNext())
      {
        string val = func((T)e.Current, index);
        if (!string.IsNullOrEmpty(val))
        {
          result.Add(val);
        }
        index++;
      }
      return string.Join(separator, result.ToArray());
    }

    /// <summary>
    /// 克隆当前集合
    /// </summary>
    /// <param name="list"></param>
    /// <<param name="funClone"></param>
    /// <returns></returns>
    public static IList Clone(this IList list, Func<object, object> funClone = null)
    {
      if (list == null)
      {
        return null;
      }
      if (funClone == null)
      {
        return new ArrayList(list);
      }
      IList result = new ArrayList(list.Count);
      foreach (object obj in list)
      {
        result.Add(funClone(obj));
      }
      return result;
    }

    /// <summary>
    /// 克隆当前集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <param name="funClone"></param>
    /// <returns></returns>
    public static IList<T> Clone<T>(this IEnumerable<T> list, Func<T, T> funClone = null)
    {
      if (list == null)
      {
        return null;
      }
      if (funClone == null)
      {
        return new List<T>(list);
      }
      IList<T> result = new List<T>();
      foreach (T obj in list)
      {
        result.Add(funClone(obj));
      }
      return result;
    }

    /// <summary>
    /// 扩展集合的Add方法
    /// </summary>
    /// <param name="list"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public static IList Add(this IList list, params object[] values)
    {
      if (list == null || values == null)
      {
        return list;
      }
      foreach (object value in values)
      {
        list.Add(value);
      }
      return list;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="list"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public static IList AddList(this IList list, ICollection values)
    {
      if (list == null || values == null)
      {
        return list;
      }
      foreach (object value in values)
      {
        list.Add(value);
      }
      return list;
    }

    /// <summary>
    /// 扩展泛型集合的Add方法
    /// </summary>
    /// <param name="list"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public static IList<T> Add<T>(this IList<T> list, params T[] values)
    {
      if (list == null || values == null)
      {
        return list;
      }
      foreach (object value in values)
      {
        list.Add((T)value);
      }
      return list;
    }

    /// <summary>
    /// 取出集合的第一个元素并移除第一个元素
    /// </summary>
    /// <param name="list">集合对象</param>
    /// <returns></returns>
    public static object Shift(this IList list)
    {
      if (list.IsNullOrEmpty())
      {
        return null;
      }
      object value = list[0];
      list.RemoveAt(0);
      return value;
    }

    /// <summary>
    /// 取出集合的第一个元素并移除第一个元素
    /// </summary>
    /// <typeparam name="T">指定类型</typeparam>
    /// <param name="list">集合对象</param>
    /// <returns></returns>
    public static T Shift<T>(this IList list)
    {
      if (list.IsNullOrEmpty())
      {
        return default(T);
      }
      return (T)list.Shift();
    }

    /// <summary>
    /// 获取集合的最后一个元素
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static object Last(this IList list)
    {
      if (list.IsNullOrEmpty())
      {
        return null;
      }
      return list[list.Count - 1];
    }

    /// <summary>
    /// 获取集合的最后一个元素
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <param name="defaultVlaue"></param>
    /// <returns></returns>
    public static T Last<T>(this IList list, T defaultValue = default(T))
    {
      if (list.IsNullOrEmpty())
      {
        return defaultValue;
      }
      T value = (T)list.Last();
      return value == null ? defaultValue : (T)value;
    }

    /// <summary>
    /// 获取集合的最后一个元素并移除最后一个
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static object Pop(this IList list)
    {
      if (list.IsNullOrEmpty())
      {
        return null;
      }
      object value = list.Last();
      list.RemoveLast();
      return value;

    }

    /// <summary>
    /// 获取集合的最后一个元素并移除最后一个
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <returns></returns>
    public static T Pop<T>(this IList list)
    {
      if (list.IsNullOrEmpty())
      {
        return default(T);
      }
      return (T)list.Pop();
    }

    /// <summary>
    /// 删除集合的第一个元素并返回集合对象
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static IList RemoveFirst(this IList list)
    {
      if (list.IsNullOrEmpty())
      {
        return null;
      }
      list.RemoveAt(0);
      return list;
    }

    /// <summary>
    /// 删除集合的最后一个元素并返回集合对象
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static IList RemoveLast(this IList list)
    {
      if (list.IsNullOrEmpty())
      {
        return null;
      }
      list.RemoveAt(list.Count - 1);
      return list;
    }


    /// <summary>
    ///
    /// </summary>
    /// <param name="list"></param>
    /// <param name="compare"></param>
    /// <returns></returns>
    public static bool IsEquals(this IList list, IList compare)
    {
      if (compare == null)
      {
        return false;
      }
      if (list.Count != compare.Count)
      {
        return false;
      }
      for (int i = 0; i < list.Count; i++)
      {
        if (!list[i].IsEquals(compare[i]))
        {
          return false;
        }
      }
      return true;
    }

    /// <summary>
    ///
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static T First<T>(this IList list, T defaultValue = default(T))
    {
      if (list.IsNullOrEmpty())
      {
        return defaultValue;
      }
      return list.First<T>(() => defaultValue);
    }

    /// <summary>
    ///
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <param name="defaultValueHandler"></param>
    /// <returns></returns>
    public static T First<T>(this IList list, Func<T> defaultValueHandler)
    {
      if (list.IsNullOrEmpty())
      {
        return defaultValueHandler();
      }
      return (T)list[0];
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="list"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static object First(this IList list, object defaultValue = null)
    {
      if (list.IsNullOrEmpty())
      {
        return defaultValue;
      }
      return list.First(() => defaultValue);
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="list"></param>
    /// <param name="defaultValueHandler"></param>
    /// <returns></returns>
    public static object First(this IList list, Func<object> defaultValueHandler = null)
    {
      if (list.IsNullOrEmpty())
      {
        return defaultValueHandler != null ? defaultValueHandler() : null;
      }
      return list[0];
    }

    /// <summary>
    /// 获取指定索引的对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <param name="idx"></param>
    /// <returns></returns>
    public static T Get<T>(this IList list, int idx, T defaultValue = default(T))
    {
      if (list == null || idx < 0 || idx >= list.Count)
      {
        return defaultValue;
      }
      return (T)list[idx];
    }

    /// <summary>
    /// 获取指定索引的对象
    /// </summary>
    /// <param name="list"></param>
    /// <param name="idx"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static object Get(this IList list, int idx, object defaultValue = null)
    {
      if (list == null || idx < 0 || idx >= list.Count)
      {
        return defaultValue;
      }
      return list[idx];
    }

    #region ToDictionaryObject
    /// <summary>
    /// 将指定的集合类的元素的指定属性值作为键转换成键值对象返回
    /// </summary>
    /// <param name="list">集合</param>
    /// <param name="keyValueProperty">作为key的值的属性，如果为空，则将集合元素本身作为键值</param>
    /// <param name="result">结果集合对象，如果为空，则默认返回ListDictionary对象</param>
    /// <returns></returns>
    public static IDictionary ToDictionaryObject(this IEnumerable list, string keyValueProperty = null, IDictionary result = null)
    {
      return list.ToDictionaryObject<object, object>((obj) => obj.GetValue(keyValueProperty));
    }

    /// <summary>
    /// 将指定的集合类的元素换成键值对象返回
    /// </summary>
    /// <param name="list">集合</param>
    /// <param name="getKeyFunc">获取键</param>
    /// <param name="result">结果集合对象，如果为空，则默认返回ListDictionary对象</param>
    /// <returns></returns>
    public static IDictionary ToDictionaryObject<TItem, TKey>(this IEnumerable list, Func<TItem, TKey> getKeyFunc = null, IDictionary result = null)
    {
      if (result == null)
      {
        result = new ListDictionary();
      }
      if (list.IsNullOrEmpty())
      {
        return result;
      }
      bool needKeyValue = (getKeyFunc != null);
      foreach (object obj in list)
      {
        if (needKeyValue)
        {
          TKey keyValue = getKeyFunc((TItem)obj);
          if (keyValue.IsNotNullOrEmpty())
          {
            result[keyValue] = obj;
          }
        }
        else
        {
          result[obj] = true;
        }
      }
      return result;
    }
    #endregion ToDictionaryObject


    #region Find扩展
    /// <summary>
    /// 查找满足条件的第一个数据
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    /// <param name="list">集合对象</param>
    /// <param name="find">条件判断函数</param>
    /// <returns></returns>
    public static T FindOne<T>(this IList list, Func<T, bool> find)
    {
      foreach (T item in list)
      {
        if (find(item))
        {
          return item;
        }
      }
      return default(T);
    }

    public static IDictionary FindOne(this IList list, string property, object value)
    {
      return list.FindOne<IDictionary>((item) =>
      {
        return item[property] == value;
      });
    }
    #endregion

    #region ToHashSet
    /// <summary>
    /// 将字符串分割后转换成HashSet &lt;string&gt;
    /// </summary>
    /// <param name="exp"></param>
    /// <param name="splitChars"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static HashSet<string> ToHashSet(this string exp, char[] splitChars = null)
    {
      var result = new HashSet<string>();
      if (exp.IsNullOrEmpty())
      {
        return result;
      }
      if (splitChars == null)
      {
        splitChars = new char[] { ',' };
      }
      foreach (var key in exp.Split(','))
      {
        result.Add(key.Trim());
      }
      return result;
    }

    /// <summary>
    /// 将制定集合的对象转换成HashSet
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <typeparam name="TSource"></typeparam>
    /// <param name="list"></param>
    /// <param name="func"></param>
    /// <returns></returns>
    public static HashSet<TResult> ToHashSet<TResult, TSource>(this IEnumerable<TSource> list, Func<TSource, TResult> func)
    {
      var result = new HashSet<TResult>();
      if (list == null)
      {
        return result;
      }
      if (func == null)
      {
        throw new NullReferenceException("参数func不能为空！");
      }
      var index = 0;
      foreach (var item in list)
      {
        if (item != null)
        {
          result.Add(func(item));
        }
        index++;
      }
      return result;
    }
    #endregion ToHashSet

    #endregion
  }
}
